package com.example.program.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import java.lang.reflect.ParameterizedType


open class BaseFragment<T : ViewBinding> : Fragment() {
    private lateinit var _binding: T
    val mBinding: T
        get() = _binding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    }

    open fun initUI(savedInstanceState: Bundle?) {

    }

    private fun initBinding():T {
        //获取泛型类型
        val type = (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0]
        val clazz = type as Class<*>

        //查找inflate方法
        val method = clazz.getMethod("inflate",LayoutInflater::class.java)
        return method.invoke(null,layoutInflater) as T
    }

//    //添加一个fragment
//    fun add(@IdRes containerViewId: Int,fragment: Fragment,addToBackStack: Boolean = true) {
//        parentFragmentManager.beginTransaction().apply {
//            setCustomAnimations(android.R.anim.fade_in,android.R.anim.fade_out,android.R.anim.fade_in,android.R.anim.fade_out)
//            add(containerViewId,fragment)
//            setReorderingAllowed(true)
//            if (addToBackStack){
//                addToBackStack(null)
//            }
//            //addSharedElement()
//            commit()
//        }
//    }

}